package ldkxingzhe.l365calendar.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.model.RepeatType;
import ldkxingzhe.l365calendar.utils.OSUtil;

/**
 * 重复选择界面
 * Created by xingzhe on 16/2/15.
 */
public class ScheduleRepeatFragment extends AllEditFragmentBase{
    @SuppressWarnings("unused")
    private static final String TAG = "ScheduleRepeatFragment";

    public static final String REPEAT_TYPE = "repeat_type";
    public static final String RESULT = "result";
    private int repeatType;
    private GridView gridView;
    private TextView hintTextView;
    private RepeatAdapter adapter;

    public static ScheduleRepeatFragment newInstance(Bundle bundle) {
        ScheduleRepeatFragment fragment = new ScheduleRepeatFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        repeatType = bundle.getInt(REPEAT_TYPE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_schedule_alarm, container, false);
        resultView.findViewById(R.id.alarm_hint).setVisibility(View.GONE);
        resultView.findViewById(R.id.alarm_desc_layout).setVisibility(View.GONE);
        gridView = (GridView) resultView.findViewById(R.id.grid_view);
        hintTextView = (TextView) resultView.findViewById(R.id.hint);
        hintTextView.setVisibility(View.VISIBLE);
        adapter = new RepeatAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                repeatType = position;
                adapter.notifyDataSetChanged();
                setIsChanged(true);
                dealWithHint();
            }
        });
        dealWithHint();
        return resultView;
    }

    private void dealWithHint() {
        hintTextView.setText(RepeatType.repeatNameOfIndex(repeatType));
    }

    @Override
    public void onSaveClick() {
        Intent resultIntent = new Intent();
        resultIntent.putExtra(RESULT, repeatType);
        getActivity().setResult(Activity.RESULT_OK, resultIntent);
        getActivity().finish();
    }

    private class RepeatAdapter extends BaseAdapter{
        private int cellWidth;

        @Override
        public int getCount() {
            return RepeatType.size();
        }

        @Override
        public String getItem(int position) {
            return RepeatType.repeatNameOfIndex(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_category, parent, false);
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(viewHolder);

                if(cellWidth <= 0){
                    cellWidth = OSUtil.getScreenWidth()/gridView.getNumColumns();
                }
                convertView.setMinimumHeight(cellWidth);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.textView.setText(getItem(position));
            if(position == repeatType){
                convertView.setBackgroundColor(Color.GRAY);
                viewHolder.textView.setTextColor(Color.BLUE);
            }else{
                convertView.setBackgroundColor(Color.WHITE);
                viewHolder.textView.setTextColor(Color.BLACK);
            }
            return convertView;
        }

        class ViewHolder{
            ImageView imageView;
            TextView textView;
        }
    }
}
